<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * 用户管理
 */
namespace Mall\Controller;
use Common\Controller\AdminController;

/**
 * 用户管理控制器
 */
class MemberController extends AdminController{
	
	/*
	 * 用户权限
	 */
	public function Auth(){
	
		action_log('admin_user_auth');//行为记录
		
		common_public()->assign_page('群组权限');
		
		//实例化公共用户群组模型
		$m_common_user_group = M('common_user_group');
		$m_common_user = M('common_user');
		$m_common_menu = M('common_menu');
		
	
		//获得所有菜单
	
		$ammap['id'] = I('param.uid');
		
		
		$amdata  = $m_common_user->where($ammap)->find();
	
	
		$cmmap['status'] =1;
		
		$channel_menu  = $m_common_menu->where($cmmap)->order('sort desc')->select();
		

		//生成相应文章链接
		foreach($channel_menu as $i=>$k){
			
			$channel_menu_map['fmid'] =  $channel_menu[$i]['id'];
			$channel_menu_map['status'] = 1;
			
			$data_channel_menu = $m_common_menu->where($channel_menu_map)->count();
			
			//判断是否有下级
			if($data_channel_menu != 0){
				
				$channel_menu_map['fmid'] =  $channel_menu[$i]['id'];
				$channel_menu_map['status'] = 1;
				
				$list_data_channel_menu = $m_common_menu->where($channel_menu_map)->select();
				
				foreach($list_data_channel_menu as $il=>$kl){
					
					$cc_channel_menu_map['fmid'] =  $list_data_channel_menu[$i]['id'];
					$cc_channel_menu_map['status'] = 1;
			
					$cc_data_channel_menu = $m_common_menu->where($cc_channel_menu_map)->count();
			
					
				}
				
				
				
			}else{
				
				$channel_menu[$i]['top'] = 1;
			}
			
		
		}
		
		
	
		$menu = unserialize($amdata['auth']);
		
		foreach($channel_menu as $i=>$k){
			
			
			foreach($menu as $ii=>$ik){
				
				if($channel_menu[$i]['id']==$menu[$ii]){
					
					
					$channel_menu[$i]['ok'] = 'checked';
					
				}
				
			}
			
			
			
		}
		
		
		$channel_menu = find_child($channel_menu);
		
		$channel_menu = _findChildren($channel_menu,0);
		

	
	
		
		$this->assign('channel_menu',$channel_menu);
		$this->assign('node_list',$channel_menu);
		
	
		$this->assign('uid',I('param.uid'));
		$this->assign('amdata',$amdata);
		

	
		$this->display();

		
		
		

	}

	
	/*
	 * 编辑用户权限
	 */
	public function AuthEdit(){
		
		action_log('admin_user_authedit');//行为记录
		
		if(IS_POST){
			
		
		
			$menu = I('param.menu','','');
			$map['id'] = I('param.uid','','');
			
			$data['auth_type'] = I('param.auth_type','','');
			
			$data['auth'] = serialize($menu);
			
			
			
			
			
			$m_common_user= M('common_user');	
			
			$result = $m_common_user->where($map)->save($data);
			
			
		
		
			if($result){
				
				 $this->success('授权成功',U('Admin/User/Auth',array('uid' =>$map['id'])));

			}else{

				
				 $this->error('授权失败');
			}

		}else{
			
			//返回信息		
			$info = $info?$info:get_errcode(-2);
	
			$this->ajaxReturn($info);
		}
		
		
		

	}
	
	/*
	 * 用户管理
	 */
    public function Manage(){
		
		action_log('admin_user_manage');//行为记录
		
		common_public()->assign_page('用户管理');
		
		
		$m_common_user = M('common_user');
		$m_common_user_group = M('common_user_group');
		$m_common_user_for_group = M('common_user_for_group');
		
	
		
		//用户状态
 		$status_info = array(
		
			'1' 	  => '启用中',
			'2' 	  => '停用中',
			
			
		);
		
		
		$status = I('param.status');
		if($status!=null){
			
			$map['status']  = $status;
			
		
			$statuss['id'] = $status;
			$statuss['title'] = $status_info[$status];
		
		}else{
			
			$map['status'] = array('neq',0);
	
		}
		
		$this->assign('statuss',$statuss);
		
		
		//判断是否搜索用户名
		$username = I('param.username');
		if($username){
			
			$map['username'] = array('like',"%$username%");

		}
		$this->assign('username',$username);
		
		//判断是否搜索用户名
		$nickname = I('param.nickname');
		if($nickname){
			
			$map['nickname'] = array('like',"%$nickname%");

		}
		$this->assign('nickname',$nickname);
		
		
		//用户UID
		$uid = I('param.uid')?I('param.uid'):'';

		if($uid!=0){
			
			$map['id'] =array('like',"%$uid%");
		}
		
		//selected="selected"
		$this->assign('uid',$uid);
		
		//判断是否搜索用户名
		$tel = I('param.tel');
		if($tel){
			
			$map['tel'] = array('like',"%$tel%");

		}
		$this->assign('tel',$tel);
		
		//判断是否搜索用户名
		$email = I('param.email');
		if($email){
			
			$map['email'] = array('like',"%$email%");

		}
		$this->assign('email',$email);
		
		//开始结束时间
		//金额区间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			
			$map['create_time']  = array(array('egt',$begin_time),array('elt',$end_time),'and'); 
			
		}else{
			
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		
		$group = $m_common_user_group->where('status=1')->order('id desc')->select();
		
		$gid= I('param.gid');
		
		
		if($gid!=0){
			
			foreach($group as $gi=>$gk){
				
				if($group[$gi]['id']==$gid){
					
					$group[$gi]['selected'] = "selected";
				}
			}
			
			$gggmap['gid'] = $gid;
			$group_list = $m_common_user_for_group->where($gggmap)->select();
			
			foreach($group_list as $gsli=>$glk){
				
				if($gsli==0){
					
					$uid = $group_list[$gsli]['uid'];
				}else{
					
					$uid = $uid.','.$group_list[$gsli]['uid'];
					
				}
			}
			
			$map['id']  = array('in',$uid);
			
		}
		
		
		$this->assign('group',$group);
		$this->assign('groups',$group);
		$this->assign('groupss',$group);
		
		
	
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		
		$list = $m_common_user->where($map)->order('status asc,create_time desc')->page($_GET['p'].',50')->select();
		$count      = $m_common_user->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,50);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		
		
		foreach($list as $li=>$lk){
			
			//显示用户群组信息
			$gmap['uid'] = $list[$li]['id'];
			$glist = $m_common_user_for_group->where($gmap)->select();
			
			foreach($glist as $gi=>$gk){
				
				$ggmap['id'] = $glist[$gi]['gid'];
				$ggdata = $m_common_user_group->where($ggmap)->find();
				
				$glist[$gi]['name'] = $ggdata['name'];
			}
			
			$list[$li]['group'] = $glist;
		
			
		}
	
		$this->assign('list',$list);
		
		
		$this->display();
	}
	
	/*
	 * 新增用户
	 */
    public function Create(){
		
		
		common_public()->assign_page('新增用户');
		
		$m_common_user = M('common_user');
		$m_common_user_group = M('common_user_group');
		$m_common_user_for_group = M('common_user_for_group');
					
			
		if(IS_AJAX){
			
			action_log('admin_user_create_create');//行为记录
		
			$data = I('');
			
			$data['password'] = md5('zyd123q');
			$data['create_time'] = time();
			$data['create_ip'] = get_client_ip();
			$data['status'] = 1;
			$result = $m_common_user->add($data);
			
			if($result){
				
				$gdata['uid'] = $result;
				$gdata['gid'] = 3;
				$gdata['type'] = 1;
				$gdata['status'] = 1;
				$m_common_user_for_group->add($gdata);
			}
			//返回信息
			$info = $result?get_errcode(0):get_errcode(10000004);
			
			$this->ajaxReturn($info);
			
		}else{
			
			action_log('admin_user_create_view');//行为记录
		
				
			$group = $m_common_user_group->where('status=1')->order('id desc')->select();
			
			$this->assign('group',$group);
				
			
			$this->display();
		}
		
		
	}
	
	/*
	 * 修改用户
	 */
    public function Edit(){
		
		common_public()->assign_page('修改信息');
		
		$m_common_user = M('common_user');
			
		$map['id'] = I('param.id');
			
		if(IS_AJAX){
			
			action_log('admin_user_edit_edit');//行为记录
		
			$data = I('');
			unset($data['id']);
			
			$result = $m_common_user->where($map)->save($data);
	
			//返回信息
			$info = $result?get_errcode(0):get_errcode(10000002);
			
			$this->ajaxReturn($info);
			
		}else{
			
			action_log('admin_user_edit_view');//行为记录
		
			$data = $m_common_user->where($map)->find();
			
			$this->assign('data',$data);
			
			$this->display();
		}
		
		
	}
	
	/*
	 * 编辑用户信息
	 */
	public function Status($id,$status=0){
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_common_user = M('common_user');
			
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					$map['id'] = $id[$i];
					
					if($status==3){
						
						action_log('admin_user_status_password');//行为记录
		
						$data['password'] = md5('zyd123q');
			
						$m_common_user->where($map)->save($data);
						
					}else{
						
						//启用或者停用用户
						if($status==1){
							
							action_log('admin_user_status_enabled');//行为记录
						
						}else{
							
							action_log('admin_user_status_outage');//行为记录
						
						}
						
						$data['status'] = $status;
			
						$m_common_user->where($map)->save($data);
					
					}
					
					
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	/*
	 * 管理用户群组
	 */
	public function Relevance(){
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_user_group = M('common_user_group');
			$m_common_user_for_group = M('common_user_for_group');
			
			$type = I('param.type');
			
			$data['uid'] = I('param.uid');
			
			$data['gid'] = I('param.group');
			
			$data['status'] = 1;
			
			
			//是增加还是删除
			if($type==1){
				
				$gmap['id'] = $data['gid'];
				
				$gdata = $m_common_user_group->where($gmap)->find();
				
				//是不是内部组
				if($gdata['type']==1){
					
					//只能同时一个内部组
					$rmap['type'] = 1;
					$rmap['uid'] = $data['uid'];
					
					
					$m_common_user_for_group->where($rmap)->delete();
					
					$data['type'] = 1;
					$result = $m_common_user_for_group->add($data);
					
					
				}else if($gdata['type']==2){
					
					$data['type'] = 2;
					$result = $m_common_user_for_group->add($data);
				}
				
				
				
				
			}else if($type==2){
				
				$result = $m_common_user_for_group->where($data)->delete();
			}
			//获得数据
			
			
			
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	
}